import time


class BaseMessage(object):
    def __init__(self, to_user, from_user):
        self._data = dict()
        self._data['ToUserName'] = to_user
        self._data['FromUserName'] = from_user
        self._data['CreateTime'] = int(time.time())


class TextMessage(BaseMessage):
    def __init__(self, to_user, from_user, content):
        BaseMessage.__init__(self, to_user, from_user)
        # super(TextMessage, self).__init__(to_user, from_user)
        self._data['Content'] = content

    def send(self):
        xml_form = """
                <xml>
                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
                <CreateTime>{CreateTime}</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[{Content}]]></Content>
                </xml>
                """
        return xml_form.format(**self._data)


class ImageMessage(BaseMessage):
    def __init__(self, to_user, from_user, media_id):
        BaseMessage.__init__(self, to_user, from_user)
        self._data['MediaId'] = media_id

    def send(self):
        xml_form = """
                <xml>
                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
                <CreateTime>{CreateTime}</CreateTime>
                <MsgType><![CDATA[image]]></MsgType>
                <Image>
                <MediaId><![CDATA[{MediaId}]]></MediaId>
                </Image>
                </xml>
                """
        return xml_form.format(**self._data)


class VoiceMessage(BaseMessage):
    def __init__(self, to_user, from_user, media_id):
        BaseMessage.__init__(self, to_user, from_user)
        self._data['MediaId'] = media_id

    def send(self):
        xml_form = """
                <xml>
                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
                <CreateTime>{CreateTime}</CreateTime>
                <MsgType><![CDATA[voice]]></MsgType>
                <Voice>
                <MediaId><![CDATA[{MediaId}]]></MediaId>
                </Voice>
                </xml>
                """
        return xml_form.format(**self._data)


class MusicMessage(BaseMessage):
    def __init__(self, to_user, from_user, title, desc, music_url, hq_url, thumb):
        super(MusicMessage, self).__init__(to_user, from_user)
        self._data['title'] = title
        self._data['desc'] = desc
        self._data['music_url'] = music_url
        self._data['hq_url'] = hq_url
        self._data['thumb'] = thumb

    def send(self):
        xml_form = """
                <xml>
                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
                <CreateTime>{CreateTime}</CreateTime>
                <MsgType><![CDATA[music]]></MsgType>
                <Music>
                <Title><![CDATA[{title}]]></Title>
                <Description><![CDATA[{desc}]]></Description>
                <MusicUrl><![CDATA[{music_url}]]></MusicUrl>
                <HQMusicUrl><![CDATA[{hq_url}]]></HQMusicUrl>
                <ThumbMediaId><![CDATA[{thumb}]]></ThumbMediaId>
                </Music>
                </xml>
                """
        return xml_form.format(**self._data)
